﻿using System.Linq;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using System;

/// <summary>
/// 修改子对象的显示列表
/// </summary>
[ExecuteInEditMode]
public class UIDisplayList : MonoBehaviour
{

    [SerializeField]
    private int[] _indexList = { };

#if UNITY_EDITOR
    bool _flag = false;
    private void OnValidate()
    {
        _flag = true;
    }

    private void Update()
    {
        if (Application.isPlaying) return;
        if (_flag)
        {
            _flag = false;
            SetDisplay(_indexList);
        }
    }

#endif
    [System.Obsolete("使用 ForEachDisplayChild ")]
    public void UpdateDisplayChild(Action<Transform> updateAction)
    {

    }

    /// <summary>
    /// 遍历所有显示的节点逻辑，执行命令
    /// </summary>
    /// <param name="updateAction"></param>
    public void ForEachDisplayChild(Action<Transform> updateAction)
    {
        foreach (Transform trans in transform)
        {
            if (trans.gameObject.activeSelf == true)
            {
                updateAction.Invoke(trans);
            }

        }
    }

    /// <summary>
    /// 显示一个节点
    /// </summary>
    /// <param name="name"></param>
    public void SetDisplay(string name)
    {
        this.SetDisplay(new string[] { name });
    }

    /// <summary>
    /// 显示一个节点
    /// </summary>
    /// <param name="index"></param>
    public void SetDisplay(int index)
    {
        this.SetDisplay(new int[] { index });
    }


    /// <summary>
    /// 获取当前正在显示的 GameObject 列表
    /// </summary>
    /// <returns></returns>
    public List<GameObject> GetDisplayObjects()
    {
        var list = new List<GameObject>();
        foreach (Transform trans in transform)
        {
            if (trans.gameObject.activeSelf)
            {
                list.Add(trans.gameObject);
            }
        }
        return list;
    }

    /// <summary>
    /// 获得当前显示列表中的第一个元素
    /// </summary>
    /// <returns></returns>
    public GameObject GetDisplayFirst()
    {
        foreach (Transform trans in transform)
        {
            if (trans.gameObject.activeSelf)
            {
                return trans.gameObject;
            }
        }
        return null;
    }

    /// <summary>
    /// 根据节点名称列表，只显示这些节点
    /// </summary>
    /// <param name="nameList"></param>
    public void SetDisplay(string[] nameList)
    {
        foreach (Transform trans in transform)
        {
            string name = trans.gameObject.name;
            if (nameList.Contains(name))
            {
                trans.gameObject.SetActive(true);
            }
            else
            {
                trans.gameObject.SetActive(false);
            }
        }
    }

    /// <summary>
    /// 只显示 该数组序号 的子节点
    /// </summary>
    /// <param name="indexList"></param>
    public void SetDisplay(int[] indexList)
    {
        foreach (Transform trans in transform)
        {
            int index = trans.GetSiblingIndex();
            if (indexList.Contains(index))
            {
                trans.gameObject.SetActive(true);
            }
            else
            {
                trans.gameObject.SetActive(false);
            }
        }
    }

}
